//
//  ALUserDefaults.swift
//  ALCore
//
//  Created by IdeaBank on 2023/12/18.
//

import Foundation

public protocol ALUserDefaultsGetAndSave {
    var key: String { get }

    var userDefaults: UserDefaults { get }

    func remove()

    func save(_ value: Any)

    func save(_ value: String)

    func save(_ value: Int)

    func save(_ value: Float)

    func save(_ value: Double)

    func save(_ value: Bool)

    func value() -> Any?

    func string() -> String?

    func int() -> Int

    func float() -> Float

    func double() -> Double

    func bool() -> Bool
}

public extension ALUserDefaultsGetAndSave {
    func remove() {
        userDefaults.removeObject(forKey: key)
    }

    func save(_ value: Any) {
        userDefaults.set(value, forKey: key)
    }

    func save(_ value: String) {
        userDefaults.set(value, forKey: key)
    }

    func save(_ value: Int) {
        userDefaults.set(value, forKey: key)
    }

    func save(_ value: Float) {
        userDefaults.set(value, forKey: key)
    }

    func save(_ value: Double) {
        userDefaults.set(value, forKey: key)
    }

    func save(_ value: Bool) {
        userDefaults.set(value, forKey: key)
    }

    func value() -> Any? {
        return userDefaults.value(forKey: key)
    }

    func string() -> String? {
        return userDefaults.value(forKey: key) as? String
    }

    func int() -> Int {
        return userDefaults.integer(forKey: key)
    }

    func float() -> Float {
        return userDefaults.float(forKey: key)
    }

    func double() -> Double {
        return userDefaults.double(forKey: key)
    }

    func bool() -> Bool {
        return userDefaults.bool(forKey: key)
    }
}
